package com.helen.ui.account;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.helen.adapter.MoneyListAdapter;
import com.helen.db.UserHelper;
import com.helen.entity.MoneyListEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.cache.converter.SerializableDiskConverter;
import com.zhouyou.http.cache.model.CacheMode;
import com.zhouyou.http.cache.model.CacheResult;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/5/22.
 * 余额金宝明细
 */

public class MoneyListActivity extends BaseActivity {
    @BindView(R.id.tv_money_type)
    TextView tvMoneyType;
    @BindView(R.id.tv_current_balance)
    TextView tvCurrentBalance;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private int type = 1;
    private int page = 1;
    private View errorView;
    private List<MoneyListEntity.ListBean> list = new ArrayList();
    private MoneyListEntity moneyListEntity;
    private MoneyListAdapter moneyListAdapter;

    //    1、余额 2、金宝
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_money_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        type = getIntent().getIntExtra("type", 2);
        if (type == 2) {
            getToolbarTitle().setText("我的金宝");
        } else {
            getToolbarTitle().setText("我的余额");
        }
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
        moneyListAdapter = new MoneyListAdapter(R.layout.item_money_list, list);
        moneyListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(MoneyListActivity.this));
        rvList.setAdapter(moneyListAdapter);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        moneyListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, rvList);


        moneyListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                MoneyListEntity.ListBean model = (MoneyListEntity.ListBean) adapter.getData().get(position);
//                Intent ti = new Intent(MoneyListActivity.this, MoneyDetailActivity.class);
//                ti.putExtra("iid", model.getId());
//                startActivity(ti);
            }
        });


        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
        page =1;
        moneyListApi();
    }


    private void loadMore() {
        page++;
        moneyListApi();
    }

    private void moneyListApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(MoneyListActivity.this).getId() + "");
        params.put("iid", type + "");
        params.put("page", page + "");
        params.put("size", "10");
        EasyHttp.post(Constants.API_JSONAPI_MONEYLIST)
                .cacheMode(CacheMode.CACHEANDREMOTEDISTINCT)
                .cacheKey(Constants.API_JSONAPI_MONEYLIST + "_" + type)
                .cacheDiskConverter(new SerializableDiskConverter())
                .params(params)
                .execute(new SimpleCallBack<CacheResult<String>>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取金宝余额列表失败==" + e.toString());
                        MToast.makeTextShort(MoneyListActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(CacheResult<String> stringCacheResult) {
                        MyLog.e("yang", "我的金宝==" + stringCacheResult);
                        int code = JsonUtil.getJsonValuesInt(stringCacheResult.data, "code");
                        String msg = JsonUtil.getJsonValuesString(stringCacheResult.data, "msg");
                        if (code == 0) {
                            Gson gson = new GsonBuilder().create();
                            moneyListEntity = gson.fromJson(stringCacheResult.data, MoneyListEntity.class);

                            if (page == 1) {
                                moneyListAdapter.setNewData(moneyListEntity.getList());
                                if (type == 2) {
                                    tvMoneyType.setText("剩余金宝");
                                    tvCurrentBalance.setText(String.format("%.2f",moneyListEntity.getJinbao()) + "");
                                    tvUnit.setText("金");
                                } else {
                                    tvMoneyType.setText("剩余余额");
                                    tvCurrentBalance.setText(String.format("%.2f",moneyListEntity.getMoney()) + "");
                                    tvUnit.setText("元");
                                }
                            } else {
                                moneyListAdapter.addData(moneyListEntity.getList());
                            }

                            if (moneyListEntity.getList().size() < 10) {
                                moneyListAdapter.loadMoreEnd(false);
                                moneyListAdapter.setEmptyView(errorView);
                            } else {
                                moneyListAdapter.loadMoreComplete();
                            }


                        } else {
                            MToast.makeTextShort(MoneyListActivity.this, msg).show();
                        }
                    }
                });
    }


}
